#include<stdio.h>
int pan(int x,int y)
{
	int c;
	c=x%y;
	while(c)//8%12==12%8
	{
		x=y;
		y=c;
		c=x%y;
	}
	return y;

}
int main()
{
	int n,a,b,c;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d %d",&a,&b);
		c=b+b;
		while(pan(a,c)!=b)
		{
			c=c+b;
		}
	printf("%d\n",c);
	}
	return 0;
}
